package jmine.tec.report.impl.table.csv;

import static jmine.tec.report.impl.table.csv.ReportTableCSVRenderer.PARAM_CSV_DATE_PATTERN;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import jmine.tec.report.api.table.ReportCell;
import jmine.tec.report.impl.table.ReportTableBuilder;

/**
 * Renderizador de células do tipo Date.
 * 
 * @author katsuo.ogawa
 */
public class DateCellCSVRenderer implements ReportCellCSVRenderer {

    /**
     * {@inheritDoc}
     */
    public void render(OutputStream outputStream, ReportCell cell, String charset) throws IOException {
        StringBuilder value = new StringBuilder();
        if (cell.getValue() != null) {
            String datePattern = cell.getParameters().getString(PARAM_CSV_DATE_PATTERN, "dd/MM/yyyy");
            SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
            value.append(dateFormat.format(Date.class.cast(cell.getValue())));
        }

        outputStream.write(value.toString().getBytes(charset));
    }

    /**
     * {@inheritDoc}
     */
    public String getRendererType() {
        return ReportTableBuilder.TYPE_DATE;
    }

}
